﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Net.Sockets;
using System.Diagnostics;

namespace PeopleCounting.Controllers
{
    public class GetCountController : Controller
    {
        //
        // GET: /GetCount/
        public ActionResult PushIndex() {
            string url = this.Request.Url.ToString();
            List<string> arr = parseQueryString(url).ToList();
            return View();
        }

        public ActionResult ClearIndex() {

            var countClear = GetFunc("/nvc-cgi/admin/vca.cgi?action=update&group=VCA.Ch0.Ct%d&count=0");
            return View();
        }
        static string[] parseQueryString(string url)
        {
            int indexStart = url.IndexOf("?");
            string toStr = url.Substring(indexStart);
            string[] sArray = toStr.Split(new char[2] { '&', '&' });
            return sArray;
        }

        public ActionResult Index()
        {
            var count = GetFunc("/cgi-bin/operator/countreport.cgi?reportfmt=table&from=now-01:00&to=now&counter=active&sampling=600&order=Ascending&value=diff");
            ViewBag.count = count;
            return View();
        }

        static string GetFunc(string param="")
        {
            string retString;
            try
            {
                String MyURI = "http://192.168.9.26"+param;
                WebRequest WReq = WebRequest.Create(MyURI);
                WReq.Credentials =
                    new NetworkCredential("root", "pass");
                WReq.Method = "GET";
                using (WebResponse response = WReq.GetResponse())
                {
                    //在这里对接收到的页面内容进行处理
                    Stream myResponseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                    retString = myStreamReader.ReadToEnd();
                }
                return retString;
            }
            catch (Exception e)
            {
                return e.InnerException.ToString();
            }
        }

    }

}
